package Chapter10;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.monte.media.Format;
import org.monte.media.FormatKeys;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.junit.Assert.*;

import java.awt.*;

import static org.monte.media.FormatKeys.*;
import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.VideoFormatKeys.*;

/**
 * Created by m.zabarylo on 10/14/2014.
 */
public class GoogleSearch {

    private WebDriver driver;
    private ScreenRecorder screenRecorder;
    private StringBuffer verificationError = new StringBuffer();

    @Before
    public void setUp() throws Exception
    {
        // Create an instance of GraphicsConfiguration to get the Graphics configuration
        // of the Screen. This is needed for ScreenRecorder class.
        GraphicsConfiguration gc = GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration();

        // Create a instance of ScreenRecorder with the required configurations
        screenRecorder = new ScreenRecorder(gc,
                new Format(MediaTypeKey, FormatKeys.MediaType.FILE, MimeTypeKey, MIME_AVI),
                new Format(MediaTypeKey, FormatKeys.MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                        CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
                        DepthKey, (int)24, FrameRateKey, Rational.valueOf(15),
                        QualityKey, 1.0f,
                        KeyFrameIntervalKey, (int) (15 * 60)),
                new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,"black",
                        FrameRateKey, Rational.valueOf(30)),
                null);

        driver = new FirefoxDriver();

        //Call the start method of ScreenRecorder to begin recording
        screenRecorder.start();
    }
    @Test
    public void testGoogleSearch() throws Exception
    {
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("q"));

        element.sendKeys("Cheese!");
        element.submit();
        try {
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                                                      public Boolean apply(WebDriver d){
                                                          return d.getTitle().toLowerCase().startsWith("cheese!");
                                                      }
                                                  }

            );
            assertEquals("Cheese! - Поиск в Google", driver.getTitle());
        } catch (Error e){
            verificationError.append(e.toString());
        }

    }
    @After
    public void tearDown() throws Exception
    {
        driver.quit();

        screenRecorder.stop();

        String verificationErrorString = verificationError.toString();
        if (!"".equals(verificationErrorString))
        {
            fail(verificationErrorString);
        }
    }
}
